﻿using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
using Microsoft.LightSwitch.Threading;
namespace LightSwitchApplication
{
    public partial class GenerateWaterBill
    {
        private string WordFile = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\WaterBill.docx";

        partial void ExportToWord_Execute()
        {
            ////*************
            ////Simple Export - simply dumps the data into a new table in word.
            //// Pass True as second parameter to use the currently open document.
            //OfficeIntegration.Word.Export(this.Books, false);
            ////*************

            //*************
            //Word.Export will also export collections into tables bookmarked in the document. 
            // For info on creating a bookmarked table see:
            // http://office.microsoft.com/en-us/word-help/add-or-delete-bookmarks-HA102269544.aspx?CTT=1
            if (!File.Exists(WordFile))
            {
                WordFile = this.GetWordFile();
            }
            if (File.Exists(WordFile))
            {
                //*************
                //Export all data to the bookmarked "PlainTable" 
                //OfficeIntegration.Word.Export(WordFile, "PlainTable", 1, true, this.GenerateElectricity);
                //*************

                //*************
                //Export only particular fields to the bookmarked "MapTable"

                List<OfficeIntegration.ColumnMapping> mappings = new List<OfficeIntegration.ColumnMapping>();
                mappings.Add(new OfficeIntegration.ColumnMapping("", "Unit"));
                mappings.Add(new OfficeIntegration.ColumnMapping("", "prevReading"));
                mappings.Add(new OfficeIntegration.ColumnMapping("", "curReading"));
                mappings.Add(new OfficeIntegration.ColumnMapping("", "month"));
                mappings.Add(new OfficeIntegration.ColumnMapping("", "year"));
                mappings.Add(new OfficeIntegration.ColumnMapping("", "amount"));
                OfficeIntegration.Word.Export(WordFile, "PlainTable", 1, true, this.GenerateWater, mappings);

                //*************


                //*************
                //Export only particular fields to the bookmarked "MapTableNoHeading", don't generate column headings and start at row 2 of the Word table
                //dynamic doc = OfficeIntegration.Word.Export(WordFile, "PlainTable", 2, false, this.GenerateElectricity, mappings);
                //*************

                //'*************
                //You must have the latest version of Adobe reader to display the PDF. Otherwise pass False as the last parameter
                //OfficeIntegration.Word.SaveAsPDF(doc, MyDocs + "\\Books.pdf", true);
                //'*************
            }
            else
            {
                this.ShowMessageBox("I can't find file: " + WordFile);
            }
        }

        #region "Find File Helper Methods"
        private string GetFile(string filter)
        {
            System.IO.FileInfo file = null;
            Dispatchers.Main.Invoke(() =>
            {
                System.Windows.Controls.OpenFileDialog dlg = new System.Windows.Controls.OpenFileDialog();
                dlg.Filter = filter;

                if (dlg.ShowDialog() == true)
                {
                    file = dlg.File;
                }
            });

            try
            {
                if (((file != null)))
                {
                    return file.FullName;
                }
                else
                {
                    return "";
                }
            }
            catch (Exception ex)
            {
                this.ShowMessageBox(ex.ToString());
                return "";
            }
        }

        private string GetWordFile()
        {
            return GetFile("Word Files (*.docx)|*.docx");
        }
        #endregion
    }
}
